package cn.lena.idle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import cn.lena.idle.dao.UserDbHelper;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.User;
import cn.lena.idle.util.PhotoUtils;

public class MyInformationActivity extends AppCompatActivity {

    private ImageView returnBtn;
    private Button update;
    private EditText uuid;
    private EditText username;
    private EditText phone;
    private EditText address;
    private ImageButton head;
    private byte[] picture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_information);

        uuid=(EditText) findViewById(R.id.uuid);
        username=(EditText)findViewById(R.id.username);
        phone=(EditText)findViewById(R.id.phone);
        address=(EditText)findViewById(R.id.address);
        head=(ImageButton)findViewById(R.id.myhead);

        // 初始化页面：显示用户信息
        uuid.setText(LoginUser.CURRENT_USER.getUuid());
        username.setText(LoginUser.CURRENT_USER.getUsername());
        phone.setText(LoginUser.CURRENT_USER.getPhone());
        address.setText(LoginUser.CURRENT_USER.getAddress());
        if(LoginUser.CURRENT_USER.getHead()!=null){
            head.setImageBitmap(BitmapFactory.decodeByteArray(LoginUser.CURRENT_USER.getHead(),0,LoginUser.CURRENT_USER.getHead().length));
        }

        // 修改
        update=(Button)findViewById(R.id.updateUser);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });

        // 返回个人中心
        returnBtn=(ImageView)findViewById(R.id.returnbefore);
        returnBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyInformationActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

        // 单击图片按钮时，能够在相册中选择图片
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,1);
            }
        });

    }


    /**
     * 选择图片后触发事件 t
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1) {
            //从相册返回的数据
            if (data != null) {
                //得到图片的全路径
                Uri uri = data.getData();
                head.setImageURI(uri);
            }
        }
    }

    /**
     * 修改账号信息
     */
    private void update(){
        User user=LoginUser.CURRENT_USER;
        user.setPhone(phone.getText().toString().trim());
        user.setAddress(address.getText().toString().trim());
        user.setHead(PhotoUtils.ImageButtonToByte(head));
        // 操作数据库
        UserDbHelper user_db=new UserDbHelper(getApplicationContext(),UserDbHelper.DB_NAME,null, Version.USER);
        boolean result = user_db.update(user);
        if (result) {
            Toast.makeText(this, "修改成功！", Toast.LENGTH_LONG).show();
            // 跳转页面到个人中心页
            Intent intent = new Intent(MyInformationActivity.this, MyActivity.class);
            startActivity(intent);
            //MyInformationActivity.this.finish();
        } else {
            Toast.makeText(this, "修改失败！", Toast.LENGTH_LONG).show();
            return;
        }
    }
}
